package vn.appsmobi.ui;

import java.util.ArrayList;

import vn.appsmobi.R;
import vn.appsmobi.activity.DetailActivity;
import vn.appsmobi.loader.BaseLoader;
import vn.appsmobi.loader.HomeLoader;
import vn.appsmobi.loader.HomeLoader.Result;
import vn.appsmobi.model.AppInfo;
import vn.appsmobi.ulti.Constant;
import vn.appsmobi.ulti.ImageLoadOption;
import vn.appsmobi.ulti.Log;
import vn.appsmobi.widget.BigAdImageView;
import vn.appsmobi.widget.EmptyLoadingView;
import vn.appsmobi.widget.MediumAdImageView;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.nostra13.universalimageloader.core.ImageLoader;

public class HomeFragment extends BaseFragment implements LoaderManager.LoaderCallbacks<HomeLoader.Result>
{
  private static final int HOME_LOADER = 0;  
  private EmptyLoadingView mLoadingView;
  private LinearLayout mHomeWrap;

  
  
  
  /**Phong
 * 
 */
public HomeFragment() {
	super();
	// TODO Auto-generated constructor stub
}

private View.OnClickListener onClick = new View.OnClickListener() {
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}
};
  
  public View onCreateView(LayoutInflater layoutFlate, ViewGroup viewgroup, Bundle bundle)
  {
	  
	Log.v("Home fragment");
    View view = layoutFlate.inflate(R.layout.fragment_home, viewgroup, false);
    mHomeWrap = (LinearLayout) view.findViewById(R.id.home_wrap);
    mHomeWrap.removeAllViews();
    
    
    /*this.mGridView = ((BaseGridView)localView.findViewById(R.id.grid_view));
    this.mHomeAdapter = new HomeAdapter(getActivity(), GRIDVIEW_COLUMN_COUNT);
    this.mGridView.setAdapter(this.mHomeAdapter);
    this.mGridView.setOnItemClickListener(this.mItemClickListner);
    this.mGridView.setOnScrollListener(new PageScrollListener(null));
    this.mGridView.setNumColumns(GRIDVIEW_COLUMN_COUNT);
    */
    this.mLoadingView = ((EmptyLoadingView)view.findViewById(R.id.loading));
    return view;
  }
  
  public void onActivityCreated(Bundle paramBundle)
  {
    super.onActivityCreated(paramBundle);
    getLoaderManager().initLoader(HOME_LOADER, null, this);
   // initLoader(CATEGORY_LOADER,null,this,getLoaderManager());
  }
  @Override
  public void onStart(){
      super.onStart();
    
  }
  
  
  
  @Override
  public Loader onCreateLoader(int id, Bundle bundle) {
  	Log.v("onCreateLoader called");
  	if (id == HOME_LOADER)
      {
        this.mLoader = new HomeLoader(getActivity());
        this.mLoader.setProgressNotifiable(this.mLoadingView);
      }
  	 BaseLoader baseLoader = this.mLoader; 
      
     return baseLoader;
  }


  @Override
  public void onLoaderReset(Loader<Result> arg0) {
  	// TODO Auto-generated method stub
  	
  }


@Override
public void onLoadFinished(Loader<Result> loader, Result data) {
	// TODO Auto-generated method stub
	 mHomeWrap.removeAllViews();
	Context context = HomeFragment.this.getActivity();
	LayoutInflater inflate = LayoutInflater.from(context);
	Log.v("onLoadFinished");
	ArrayList<AppInfo> mAppInfos = data.mHomeInfos;
	int totalApp = mAppInfos.size();
	int totalItem = (int) Math.ceil((double)totalApp / 5);// () ;
	
	int index = 0;
	for(int i = 0; i < totalItem;i++)
	{		
		index = i*5;
		View item = inflate.inflate(R.layout.home_list_item,null);
    	
	try{			
			AppInfo app1 = mAppInfos.get(index);
			
			if(app1 != null)
			{
				BigAdImageView big1 = (BigAdImageView)item.findViewById(R.id.big_ad1);
				big1.setVisibility(View.VISIBLE);
				big1.setOnClickListener(new ItemOnClickListener(app1));
				ImageLoader.getInstance().displayImage(app1.getLinkHome(),big1,ImageLoadOption.options);
			}
			
			AppInfo app2 = mAppInfos.get(index+1);
			
			if(app2 != null)
			{
				MediumAdImageView medium1 = (MediumAdImageView)item.findViewById(R.id.medium_ad1);
				medium1.setVisibility(View.VISIBLE);
				medium1.setOnClickListener(new ItemOnClickListener(app2));
				ImageLoader.getInstance().displayImage(app2.getLinkHome(),medium1,ImageLoadOption.options);
			}
			
			
			AppInfo app3 = mAppInfos.get(index+2);
			
			if(app3 != null)
			{
				MediumAdImageView medium2 = (MediumAdImageView)item.findViewById(R.id.medium_ad2);
				medium2.setVisibility(View.VISIBLE);
				medium2.setOnClickListener(new ItemOnClickListener(app3));
				ImageLoader.getInstance().displayImage(app3.getLinkHome(),medium2,ImageLoadOption.options);
			}
			
			AppInfo app4 = mAppInfos.get(index+3);
			
			if(app4 != null)
			{
				MediumAdImageView medium3 = (MediumAdImageView)item.findViewById(R.id.medium_ad3);
				medium3.setVisibility(View.VISIBLE);
				medium3.setOnClickListener(new ItemOnClickListener(app4));
				ImageLoader.getInstance().displayImage(app4.getLinkHome(),medium3,ImageLoadOption.options);
			}
			
			AppInfo app5 = mAppInfos.get(index+4);
			
			if(app5 != null)
			{
				MediumAdImageView medium4 = (MediumAdImageView)item.findViewById(R.id.medium_ad4);
				medium4.setVisibility(View.VISIBLE);
				medium4.setOnClickListener(new ItemOnClickListener(app5));
				ImageLoader.getInstance().displayImage(app5.getLinkHome(),medium4,ImageLoadOption.options);
			}
			
		}catch(Exception e){
			e.printStackTrace();
		}		
		mHomeWrap.addView(item,i);
			
	}
	
	    	       
    
	 
}
	
	class ItemOnClickListener implements OnClickListener
	{
	
	  AppInfo mAppinfo;
	  public ItemOnClickListener(AppInfo appinfo) {
	      mAppinfo = appinfo;
	  }
	
	  @Override
	  public void onClick(View v)
	  {
		  Intent intent = new Intent(HomeFragment.this.getActivity(),DetailActivity.class);
      	  intent.putExtra(Constant.INTENT_EXTRA_APP_ID, mAppinfo.getId());
          intent.putExtra(Constant.INTENT_EXTRA_APP_NAME, mAppinfo.getName());
          intent.putExtra(Constant.INTENT_EXTRA_APP_CAT, mAppinfo.getCategory());
          intent.putExtra(Constant.INTENT_EXTRA_APP_SIZE, mAppinfo.getSize());            
          intent.putExtra(Constant.INTENT_EXTRA_APP_AGENCY, mAppinfo.getCompanyName());
          intent.putExtra(Constant.INTENT_EXTRA_APP_ICON, mAppinfo.getIconLink());
          //intent.putExtra(Constant.INTENT_EXTRA_APP_RATE, appInfo.get);
          
          HomeFragment.this.getActivity().startActivity(intent);
          HomeFragment.this.getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.disappear);
	  }
	
	};
	

}

